package org.lboot.flow.processor.impl;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.flow.constant.FlowNodeTypeConst;
import org.lboot.flow.constant.FlowWorkStatusConst;
import org.lboot.flow.constant.FlowWorkStatusEnum;
import org.lboot.flow.module.work.FlowWork;
import org.lboot.flow.module.work.FlowWorkService;
import org.lboot.flow.module.work.params.FlowTaskParams;
import org.lboot.flow.processor.FlowNodeProcessor;
import org.lboot.flow.event.system.FlowEndEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author kindear
 */
@Slf4j
@Service
@AllArgsConstructor
public class EndNodeProcessorImpl implements FlowNodeProcessor {

    FlowWorkService flowWorkService;

    @Resource
    ApplicationContext context;

    @Override
    public String getNodeType() {
        return FlowNodeTypeConst.END_NODE;
    }

    @Override
    public void process(FlowWork flowWork, FlowTaskParams taskParams) {
        // 更新流程状态
        flowWork.setFlowWorkStatus(FlowWorkStatusEnum.FINISHED.value);
        flowWork.setStatus(0);
        flowWorkService.updateOne(flowWork);
        // 触发流程END事件
        context.publishEvent(new FlowEndEvent(this,flowWork));
    }

    @Override
    public void back(FlowWork flowWork, FlowTaskParams taskParams) {

    }
}
